import { addRoute, MiddlewareCreater, Route } from 'f2e-server3'
import { server_info, app, begin_cpu_percent } from './apis'

addRoute('/api/server/info', server_info, { type: 'sse', interval: 1000 })

export const app_serve: MiddlewareCreater = {
    name: 'app_serve',
    mode: ['dev', 'prod'],
    execute (conf) {
        const route = new Route(conf)
        // 需要从这里执行，否则会阻塞build任务
        begin_cpu_percent()
        route.on('/api1/server/info', server_info, { type: 'sse', interval: 1000 })
        route.on('/api1/server/shutdown', app.shutdown)
        return {
            onRoute: route.execute,
        }
    },
}

export default app_serve